<template>
  <div class="app-container">
    <div>
      <p class="dialogTitle title">当前地震事件:
        <span v-if="currentEvent">{{ currentEvent.eventName }}</span>
        <span v-else>请在地震事件管理中设置当前地震事件</span>
      </p>
    </div>

    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px" v-show="showSearch">
      <el-form-item label="通知名称" prop="noticeName">
        <el-input v-model="queryParams.noticeName" placeholder="请输入通知名称" clearable />
      </el-form-item>
      <el-form-item label="通知类型" prop="noticeType">
        <el-select v-model="queryParams.noticeType" placeholder="请选择通知类型" clearable>
            <el-option v-for="item in option"
                :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" :disabled="!currentEvent" @click="handleAdd">新建</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" stripe>
      <el-table-column label="序号" align="center" width="55">
        <template #default="scope">
          <span>{{ (queryParams.page - 1) * queryParams.rows + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通知编号" align="center" prop="noticeCode" show-overflow-tooltip >
        <template #default="scope">
          <span style="color: #107ce1;cursor: pointer;" @click="handleEdit1(scope.row)">{{ scope.row.noticeCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通知名称" align="center" prop="noticeName" />
      <el-table-column label="通知类型" align="center" prop="noticeType" width="100px">
        <template #default="scope">
          <span>{{ getNoticeType(scope.row.noticeType) || scope.row.noticeType }}</span>
        </template>  
      </el-table-column>
      <el-table-column label="通知范围" align="center" prop="noticeRange" />
      <el-table-column label="发起人" align="center" prop="sponsor" />
      <el-table-column label="创建日期" align="center" prop="noticeTime" />
      <el-table-column label="操作" align="center" width="100px">
        <template #default="scope">
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
      @pagination="getList" />

    <el-dialog :title="title" v-if="open" v-model="open" width="900px" append-to-body>
      <el-form ref="addRef" label-width="120px" :model="form" :rules="rules">
        <el-row style="margin: 20px 0;">
          <el-col :span="12">
            <el-form-item label="通知类型" prop="noticeType">
              <el-select v-model="form.noticeType" placeholder="请选择通知类型" style="width:100%" clearable>
                <el-option v-for="item in option"
                    :key="item.dictItemCode" :label="item.dictItemName" :value="item.dictItemCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通知编号" prop="noticeCode">
              <el-input v-model="form.noticeCode" placeholder="请输入通知编号" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通知名称" prop="noticeName">
              <el-input v-model="form.noticeName" placeholder="请输入通知名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="通知内容" prop="noticeContent">
              <el-input v-model="form.noticeContent" type="textarea" maxlength="500" show-word-limit
                :autosize="{ minRows: 5, maxRows: 5 }" placeholder="请输入通知内容" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发起人" prop="sponsor" required>
              <el-input v-model="form.sponsor" placeholder="请输入发起人" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="noticeTime" required>
              <el-input v-model="form.noticeTime" placeholder="请输入创建时间" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="通知范围" prop="noticeRange">
                <el-radio-group v-model="form.noticeRange">
                  <el-radio label="全部现场人员">全部现场人员</el-radio>
                  <el-radio label="全部现场调查人员">全部现场调查人员</el-radio>
                  <el-radio label="按调查组勾选">按调查组勾选</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-table :data="dataList1" ref="tableRef" stripe v-if="form.noticeRange=='按调查组勾选'" row-key="xitId" @selection-change="handleSelectionChange">
              <el-table-column type="selection" align="center" :reserve-selection="true" width="55" />
              <el-table-column label="序号" align="center" width="55">
                <template #default="scope">
                  <span>{{ (queryParams1.page - 1) * queryParams1.rows + scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="调查组编号" align="center" prop="teamCode" show-overflow-tooltip >
                <template #default="scope">
                  <span style="color: #107ce1;cursor: pointer;" @click="handleEdit(scope.row)">{{ scope.row.teamCode }}</span>
                </template>
              </el-table-column>
              <el-table-column label="调查组名称" align="center" prop="teamName" />
              <el-table-column label="组长" align="center" prop="leader" />
              <!-- <el-table-column label="组长联系方式" align="center" prop="leaderPhone" />
              <el-table-column label="创建单位" align="center" prop="company" />
              <el-table-column label="创建人" align="center" prop="createUser" />
              <el-table-column label="创建日期" align="center" prop="createTime" /> -->
            </el-table>

            <pagination v-if="form.noticeRange=='按调查组勾选'" v-show="total1 > 0" :total="total1" v-model:page="queryParams1.page" v-model:limit="queryParams1.rows"
              @pagination="getList1" />
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="save">保 存</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="查看小组" v-model="open1" width="900px" append-to-body>
      <el-form ref="addRef1" label-width="120px" :model="form1">
        <el-row style="margin: 20px 0;">
          <el-col :span="12">
            <el-form-item label="调查组编号" prop="teamCode">
              <el-input v-model="form1.teamCode" placeholder="请输入调查组编号" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调查组名称" prop="teamName">
              <el-input v-model="form1.teamName" placeholder="请输入调查组名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组长" prop="leader">
              <el-input v-model="form1.leader" placeholder="请选择组长" :suffix-icon="MoreFilled" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组长联系方式" prop="leaderPhone" required>
              <el-input v-model="form1.leaderPhone" placeholder="请输入组长联系方式" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调查组描述" prop="teamName">
              <el-input v-model="form1.teamName" type="textarea" maxlength="500" show-word-limit
                :autosize="{ minRows: 5, maxRows: 5 }" placeholder="请输入调查组描述" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人单位" prop="company" required>
              <el-input v-model="form1.company" placeholder="请输入创建单位" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人" prop="createUser" required>
              <el-input v-model="form1.createUser" placeholder="请输入创建人" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime" required>
              <el-input v-model="form1.createTime" placeholder="请输入创建时间" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-tabs v-model="activeName" class="demo-tabs" type="card">
        <el-tab-pane label="组员" name="first">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" disabled>添加组员</el-button>
            </el-col>
          </el-row>
          <el-table :data="form1.personnel" stripe>
            <el-table-column label="序号" type="index" align="center" width="55" />
            <el-table-column label="姓名" align="center" prop="personName" />
            <el-table-column label="性别" align="center" prop="gender" />
            <el-table-column label="手机号" align="center" prop="tel" />
            <el-table-column label="单位" align="center" prop="unit" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="司机" name="second">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" disabled>添加司机</el-button>
            </el-col>
          </el-row>
          <el-table :data="form1.driver" stripe>
            <el-table-column label="序号" type="index" align="center" width="55" />
            <el-table-column label="姓名" align="center" prop="personName" />
            <el-table-column label="性别" align="center" prop="gender" />
            <el-table-column label="手机号" align="center" prop="tel" />
            <el-table-column label="单位" align="center" prop="unit" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open1 = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="informManage">
import useEventStore from "@/store/modules/event";
import { getAllList1, saveData, getDetail1, updateData, getCreateInfo, del } from "@/api/fieldWork/informManage.js"
import { getAllList,getDetail } from "@/api/fieldWork/surveyManage.js"
import { MoreFilled } from '@element-plus/icons-vue'
import { getDict } from "@/api/system/dict";
const currentEvent = computed(() => useEventStore().currentEvent);
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const dataList1 = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const total1 = ref(0);
const showSearch = ref(true);
const open = ref(false);
const title = ref();
const activeName = ref('first');
const open1 = ref(false);
const selectionData = ref([]);
const option = ref([]);
const data = reactive({
  queryParams: {
    page: 1,
    rows: 10,
    noticeName: undefined,
    noticeType: undefined,
  },
  queryParams1: {
    page: 1,
    rows: 10,
  },
  form: {
  },
  form1: {
  },
  rules: {
    noticeType: [{ required: true, message: "通知类型不能为空", trigger: "change" }],
    noticeCode: [{ required: true, message: "通知编号不能为空", trigger: "blur" }],
    noticeName: [{ required: true, message: "通知名称不能为空", trigger: "blur" }],
    noticeContent: [{ required: true, message: "通知内容不能为空", trigger: "blur" }],
    sponsor: [{ required: true, message: "发起人不能为空", trigger: "blur" }],
    noticeTime: [{ required: true, message: "创建时间不能为空", trigger: "blur" }],
    noticeRange: [{ required: true, message: "通知范围不能为空", trigger: "change" }],
  }
})
const { queryParams, queryParams1, form, rules, form1 } = toRefs(data)

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    // single.value = selection.length != 1;
    // multiple.value = !selection.length;
    ids.value = selection.map((item) => item.xitId);
    selectionData.value = selection;
};

// 新增
function handleAdd() {
  title.value = '新建通知信息';
  queryParams1.value = {
    page: 1,
    rows: 10,
  },
  getList1();
  getInfo();
}
function getInfo() {
  getCreateInfo({ xsemId: currentEvent.value.id }).then(res => {
    open.value = true;
    proxy.resetForm("addRef");
    form.value = res.data;
    form.value.personnel = [];
    form.value.driver = [];
  })
}

// 编辑
function handleEdit1(row) {
  title.value = '编辑通知信息'
  detail(row.xnmId);
}

// 查看
function detail(id) {
  queryParams1.value = {
    page: 1,
    rows: 10,
  },
  getList1();
  getDetail1({ id: id }).then(res => {
    open.value = true;
    proxy.resetForm("addRef");
    form.value = res.data;
    if(form.value.noticeRange=='按调查组勾选'){
      form.value.teamList.forEach(item=>{
        nextTick(()=>{
          proxy.$refs['tableRef'].toggleRowSelection(item,true)
        })
      })
    }
  })
}


// 编辑
function handleEdit(row) {
  detail1(row.xitId);
}
// 查看
function detail1(id) {
  getDetail({ id: id }).then(res => {
    open1.value = true;
    activeName.value = 'first';
    proxy.resetForm("addRef1");
    form1.value = res.data;
  })
}
// 保存
function save() {
  proxy.$refs["addRef"].validate(valid => {
    if (valid) {
      if(form.value.noticeRange=='按调查组勾选'&&selectionData.value.length==0){
        proxy.$modal.msgWarning('请勾选调查小组')
        return;
      }
      if(form.value.noticeRange=='按调查组勾选'){
        form.value.teamList = selectionData.value;
      }else{
        form.value.teamList = null;
      }
      if (title.value == '新建通知信息') {
        saveData({ ...form.value, xsemId: currentEvent.value.id }).then(res => {
          proxy.$modal.msgSuccess("新建成功");
          open.value = false;
          getList();
        })
      } else {
        updateData({ ...form.value, xsemId: currentEvent.value.id }).then(res => {
          proxy.$modal.msgSuccess("保存成功");
          open.value = false;
          getList();
        })
      }
    }
  })

}

/** 删除按钮操作 */
function handleDelete(row) {
  console.log(row);
  proxy.$modal
    .confirm("是否确认删除该信息，请确认是否要删除？",)
    .then(function () {
      return del(row.xnmId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dataList.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 查询数据列表 */
function getList() {
  if (currentEvent.value) {
    loading.value = true;
    getAllList1({ ...queryParams.value, xsemId: currentEvent.value.id }).then(response => {
      dataList.value = response.data.rows ? response.data.rows : [];
      total.value = response.data.total;
      loading.value = false;
    });
  } else {
    dataList.value = [];
    total.value = 0;
  }
}

/** 查询数据列表 */
function getList1() {
  if (currentEvent.value) {
    getAllList({ ...queryParams1.value, xsemId: currentEvent.value.id }).then(response => {
      dataList1.value = response.data.rows ? response.data.rows : [];
      total1.value = response.data.total;
    });
  } else {
    dataList1.value = [];
    total1.value = 0;
  }
}

/** 获取字典值 */
async function getDictData() {
  await getDict("tzlx").then(response => {
	    option.value = response.data;
	});
}
function getNoticeType(val){
	var name = ''
	option.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}

onMounted(() => {
  setTimeout(() => {
    getList();
    getList1();
  }, 200)
})
getDictData();
</script>

<style scoped lang="scss">
.title {
  margin: 0 0 20px !important;
}
</style>

